'''
dionysus - a web based dialogue management framework
Copyright (C) 2011  Eugeniu Costetchi <eugeniu.costetchi (at) tudor.lu>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Created on Jan 25, 2011
'''
from DialogueManager.Parser import InputTypes
from DialogueManager.planning.Operator import *
from DialogueManager.planning.PlanFragment import PlanFragment
from DialogueManager.planning.Action import *

#if __name__ == '__main__':
#    pass

def build_qaf_instance(qaf_about, place_holders=None):
    '''builds the qaf instance from existing qaf template'''
    while_condition = Not(Or(QafAnswerTypeCompatible(), IsInputType(InputTypes.NONE)))
    
    qaf_instance_fragment = PlanFragment() + [InstantiateQafFromTemplatePool(qaf_about, place_holders),
                                            AskQafQuestion(), ListenInteractionAnction()]
    qaf_instance_fragment = qaf_instance_fragment.add_while(while_condition,
                                 PlanFragment() + [SayTemplateInteractionAction('${name}, \
                                 I could not understand what you said before, can you please express \
                                 your answer differently?'),
                                 AskQafQuestion(),
                                 ListenInteractionAnction()])
    
    qaf_instance_fragment = qaf_instance_fragment.add_condition(condition=QafAnswerTypeCompatible(),
                                                                then_fragment=PlanFragment() + [
                                                                                AcceptAnswer(),
                                                                                AccomodateQafFormalExpression(),
                                                                                EradicateQaf()],
                                                                else_fragment=PlanFragment() + EradicateQaf())
    return qaf_instance_fragment

def build_persuasive_listen_for_type(type=InputTypes.LOW, to_variable=None):
    pf = PlanFragment() + ListenInteractionAnction(to_variable)
    seq = PlanFragment() + SayInteractionAction('I could not understand, can you please express \
                                 your answer differently?')
    seq += ListenInteractionAnction(to_variable)
    return  pf.add_while(And(Not(IsInputType(InputTypes.NONE)), Not(IsInputType(type))), seq)

